package com.fhz.circle.register.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.fhz.circle.R;
import com.fhz.circle.databinding.ActivityRegisterBinding;
import com.fhz.circle.manager.DialogManager;
import com.fhz.circle.register.data.User;
import com.fhz.circle.widget.DialogView;
import com.fhz.circle.widget.LoadingView;
import com.fhz.circle.widget.TouchPictureV;
import com.fhz.core_lib.mvvm.BaseMVVMActivity;

/**
 * @author: fhz
 * @describe: 注册
 * @date: 2021-11-14 13:05:06
 */

public class RegisterActivity extends BaseMVVMActivity<RegisterViewModel, ActivityRegisterBinding> implements View.OnClickListener{

    private  DialogView dialog;
    private TouchPictureV touchPictureV;

    private LoadingView loadingView;
    @Override
    protected RegisterViewModel createViewModel() {
        return ViewModelProviders.of(this,new RegisterViewModelFactory()).get(RegisterViewModel.class);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        binding.registerVerificationBtn.setOnClickListener(this);
        binding.registerBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register_verification_btn:
                //点击验证码的逻辑
                dialog = DialogManager.getInstance().createDialog(this, R.layout.dialog_layout);
                touchPictureV = dialog.findViewById(R.id.touch_picture);
                DialogManager.getInstance().show(dialog);
                touchPictureV.setViewResultListener(new TouchPictureV.OnViewResultListener() {
                    @Override
                    public void onResult() {
                        binding.registerBtn.setEnabled(true);
                        //发送短信
                        DialogManager.getInstance().hide(dialog);
                    }
                });
                break;
            case R.id.register_btn:
                loadingView = new LoadingView(this);
                loadingView.show();
                //发送注册请求
                String nickname = binding.registerUsername.getText().toString();
                String password = binding.registerPassword.getText().toString();
                String phone = binding.registerPhone.getText().toString();

                User user = new User();
                user.setNickname(nickname);
                user.setPassword(password);
                user.setPhoneNumber(phone);

                viewModel.registerUser(user).observe(this, new Observer<User>() {
                    @Override
                    public void onChanged(User user) {
                        Toast.makeText(RegisterActivity.this, "haha"+user.getPassword(), Toast.LENGTH_SHORT).show();
                    }
                });

                break;
        }
    }

    @Override
    protected void initData() {
//        viewModel.getUserMutableLiveData().observe(this, new Observer<User>() {
//            @Override
//            public void onChanged(User user) {
//                Toast.makeText(RegisterActivity.this, "hah"+user.getNickname(), Toast.LENGTH_SHORT).show();
//            }
//        });

    }
}
